// for...of 循环可以使用的范围包括：

// 数组
// Set
// Map
// 类数组对象，如 arguments 对象、DOM NodeList 对象
// Generator 对象
// 字符串

// 模拟实现for of方法
; (function () {
    //return
    function forOf(obj, cb) {
        let iterable, result;

        // 先判断属性[Symbol.iterator]
        if (typeof obj[Symbol.iterator] !== "function")
            throw new TypeError(result + " is not iterable");
        if (typeof cb !== "function") throw new TypeError("cb must be callable");

        iterable = obj[Symbol.iterator]();

        result = iterable.next();
        while (!result.done) {
            cb(result.value);
            result = iterable.next();
        }
    }

})();

//为数组特别增加的迭代器方法
; (function () {
    //return
    var colors = ["red", "green", "blue"];

    for (let index of colors.keys()) {
        console.log(index);
    }

    // 0
    // 1
    // 2

    for (let color of colors.values()) {
        console.log(color);
    }

    // red
    // green
    // blue

    for (let item of colors.entries()) {
        console.log(item);
    }

    // [ 0, "red" ]
    // [ 1, "green" ]
    // [ 2, "blue" ]

})();
// Set的的迭代打印
; (function () {
    //return
    var colors = new Set(["red", "green", "blue"]);

    for (let index of colors.keys()) {
        console.log(index);
    }

    // red
    // green
    // blue

    for (let color of colors.values()) {
        console.log(color);
    }

    // red
    // green
    // blue

    for (let item of colors.entries()) {
        console.log(item);
    }

    // [ "red", "red" ]
    // [ "green", "green" ]
    // [ "blue", "blue" ]

})();
// set和map
; (function () {
    //return
    const values = new Set([1, 2, 3]);

    for (let value of values) {
        console.log(value);
    }
    // 1
    // 2
    // 3

    const values2 = new Map([["key1", "value1"], ["key2", "value2"]]);
    for (let value of values2) {
        console.log(value);
    }

    // ["key1", "value1"]
    // ["key2", "value2"]




})();